package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.passport.pojo.param.AdminInsertParam;
import cn.tedu.csmall.passport.pojo.param.AdminLoginInfoParam;
import cn.tedu.csmall.passport.pojo.param.AdminUpdateInfoParam;
import cn.tedu.csmall.passport.pojo.vo.AdminListVO;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.security.CurrentPrincipal;
import cn.tedu.csmall.passport.service.AdminService;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.commons.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@RestController
@RequestMapping("/admin")
@Api(tags = "04.管理员管理模块")
@Slf4j
@Validated
public class AdminController {

    @Autowired
    AdminService service;
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    public JsonResult login(AdminLoginInfoParam adminLoginInfoParam){
        log.debug("{}",adminLoginInfoParam);
        String jwt = service.login(adminLoginInfoParam);
        return JsonResult.ok(jwt);
    }



    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    public JsonResult insert(@Valid AdminInsertParam adminInsertParam){
            service.insert(adminInsertParam);
            return JsonResult.ok();
    }

    @GetMapping(value = "/getStandard")
    @ApiOperation("查询管理员详情")
    @ApiOperationSupport(order = 402)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long")
    })
    public JsonResult getStandard(@Range(min = 1,message = "根据ID查看管理员详情失败，请提交合法的ID值")
                                  @RequestParam Long id) throws Exception{

        return JsonResult.ok(service.getStandardById(id));
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 400)
    public JsonResult list(Integer pageNum){
        PageData<AdminListVO> list = service.list(pageNum);
        return JsonResult.ok(list);
    }

    @GetMapping(value = "/listThis")
    @ApiOperation("查询当前管理员")
    @ApiOperationSupport(order = 401)

    public JsonResult list(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        log.debug("用户名：{}",currentPrincipal.getUsername());
        log.debug("id：{}",currentPrincipal.getId());

        return JsonResult.fail(ServiceCode.ERR_UNKNOWN,"此功能尚未开发");
    }

    @PostMapping(value = "/delete")
    @ApiOperation("删除管理员")
    @ApiOperationSupport(order = 200)
    public JsonResult deleteById(Long id){
        service.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/updateInfo")
    @ApiOperation("修改管理员基本信息")
    @ApiOperationSupport(order = 300)
    public JsonResult update(@Valid AdminUpdateInfoParam adminUpdateInfoParam){
        service.update(adminUpdateInfoParam);
        return JsonResult.ok();
    }

    @PostMapping("/updatePassword")
    @ApiOperation("修改管理员密码")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,paramType = "query",dataType = "long"),
            @ApiImplicitParam(name = "password", value = "密码", required = true,paramType = "query")
    })
    public JsonResult update(@RequestParam Long id,@RequestParam String password){
        service.update(id,password);
        return JsonResult.ok();
    }
}
